package com.qiaoanju.web.back.controller.system;

import com.qiaoanju.back.domain.system.DictModel;
import com.qiaoanju.back.domain.system.MenuModel;
import com.qiaoanju.back.domain.system.UserModel;
import com.qiaoanju.back.service.system.IMenuService;
import com.qiaoanju.commons.constants.ErrorCodes;
import com.qiaoanju.commons.constants.SessionConstant;
import com.qiaoanju.commons.constants.StringConstant;
import com.qiaoanju.commons.enums.ResultCode;
import com.qiaoanju.commons.utils.DateUtil;
import com.qiaoanju.commons.utils.RedisUtil;
import com.qiaoanju.commons.utils.StringUtil;
import com.qiaoanju.web.controller.BaseController;
import com.qiaoanju.web.security.service.LoginUserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/sysmenu")
public class MenuController extends BaseController {
	
	@Autowired
	private IMenuService service ;

	@Autowired
	private RedisUtil redisUtil ;

	@RequestMapping(value = "/index", method = { RequestMethod.GET })
	public String list(Model model, HttpServletRequest request,
                       HttpServletResponse response) throws Exception {

		//获取登录用户的信息
		model.addAttribute(SessionConstant.SESSION_USER, LoginUserService.getLoginUser());
        //获取父菜单
		MenuModel parentMenu = service.getMenuById(1);
		logger.info("************************************"+ parentMenu.toString());
		model.addAttribute("parentMenu",parentMenu);

		//获取子菜单
		List<MenuModel> secondMenus = service.getchildByPid(parentMenu.getId());
		logger.info("************************************"+ secondMenus.size());
		model.addAttribute("secondMenus",secondMenus);

		//获取全部的菜单
		List<MenuModel> mainTreelists = service.getAllMenus(1);
		logger.info("************************************"+ mainTreelists.size());

		model.addAttribute("mainTreelists",mainTreelists);

		List<MenuModel> lists = service.getMenusList();

		model.addAttribute("list",lists);

		return "sys/menu/list";

	}


	@RequestMapping(value = "/list", method = { RequestMethod.POST})
	public void getList(HttpServletRequest request,
                        HttpServletResponse response, MenuModel menu) throws Exception {

		JSONArray evtArray = new JSONArray();
		List<MenuModel> lists = service.getMenusList();
		for (MenuModel model: lists) {
			JSONObject json  = new JSONObject();
			json.put("id",model.getId());
			json.put("menu_name",model.getMenu_name());
			json.put("create_tima",model.getCreate_time());

			evtArray.add(json);

		}

		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", evtArray);//JSONArray
		jsonObject.put("total",100);//总记录数
		printJson(response,ResultCode.Success,jsonObject.toString());

	}

	@RequestMapping(value = "/insert", method = { RequestMethod.POST})
	@ResponseBody
	public void add(Model model, HttpServletRequest request,
					HttpServletResponse response, MenuModel menu){

		//
		menu.setEnable(1);
		menu.setMenu_level(1);
		menu.setCreate_id(LoginUserService.getLoginUserId());
		menu.setUpdate_time(DateUtil.getCurrentDate());

		//后台校验
		logger.info("***********************model:"+menu);
		int result = service.insert(menu);
		//处理结果
		if (result > 0){

			printJson(response, ResultCode.Success, StringConstant.INSERT_MSG);
		}else{
			printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
		}
	}


	@RequestMapping(value = "/deleteByIds", method = { RequestMethod.POST})
	@ResponseBody
	public void deleteById(Model model, HttpServletRequest request,
						   HttpServletResponse response, String ids){
/*		int result = 0 ;
		if(!StringUtils.isBlank(ids)){
			int[] idArr = StringUtil.getIntArryFromString(ids,",");
			if(idArr.length > 1){
				result = service.deleteByIds(idArr);
			}else{
				result = service.deleteById(idArr[0]);
			}

			//处理结果
			if (result > 0){
				printJson(response, ResultCode.Success, StringConstant.DELETE_MSG);
			}else{
				printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
			}
		}else{
			printJson(response, ErrorCodes.PARAM_NULL, StringConstant.PARAM_NULL_MSG);
		}*/

	}


	@RequestMapping(value = "/editModel", method = { RequestMethod.GET})
	@ResponseBody
	public void editModel(Model model, HttpServletRequest request,
						  HttpServletResponse response, int id){
		int result = 0 ;
		if(id != 0){
			MenuModel menu = service.getMenuById(id);
			//处理结果
			if (menu != null){
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("data", menu);
				jsonObject.put("msg", StringConstant.FORMAT_DATA_SUCCESS_MSG);
				printJson(response,ResultCode.Success,jsonObject);
			}else{
				printJson(response,ErrorCodes.PARAM_ERROR, StringConstant.PARAM_ERROR_MSG);
			}
		}else{
			printJson(response,ErrorCodes.PARAM_NULL, StringConstant.PARAM_NULL_MSG);
		}

	}


	@RequestMapping(value = "/edit", method = { RequestMethod.POST})
	@ResponseBody
	public void edit(Model model, HttpServletRequest request,
					 HttpServletResponse response, MenuModel menu){
		boolean result = false;
		if(menu != null && menu.getId() != 0){
			MenuModel editModel = service.getMenuById(menu.getId());

			menu.setUpdate_id(LoginUserService.getLoginUserId());
			menu.setUpdate_time(DateUtil.getCurrentDate());
			result = service.edit(menu);
			//处理结果
			if (result){
				printJson(response, ResultCode.Success, StringConstant.UPDATE_MSG);
			}else{
				printJson(response,ResultCode.Fail, StringConstant.OPERATE_FAILURE_MSG);
			}
		}else{
			printJson(response,ErrorCodes.PARAM_ERROR, StringConstant.PARAM_ERROR_MSG);
		}

	}

}
